package com.shanjiduo.newsline.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.github.lianghanzhen.LazyFragmentPagerAdapter;
import com.github.lianghanzhen.LazyViewPager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.shanjiduo.newsline.R;

/**
 * Created by donglinghao on 2016-01-28.
 */
public class ReportFragment extends Fragment {

    static SlidingFragmentActivity sld;
    private View mRootView;
    private LazyViewPager lvp;
    private PagerSlidingTabStrip pst;

    public ReportFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance(SlidingFragmentActivity sld1) {
        HomeFragment fragment = new HomeFragment();
        sld = sld1;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            Log.e("666", "ReportFragment");
            mRootView = inflater.inflate(R.layout.report_fragment, container, false);
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        initView(mRootView);
        return mRootView;
    }

    private void initView(View mRootView) {
        lvp = (LazyViewPager) mRootView.findViewById(R.id.moview_pager);
        pst = (PagerSlidingTabStrip) mRootView.findViewById(R.id.movie_tabs);
        lvp.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        pst.setViewPager(lvp);
        pst.setTextColor(Color.RED);
        pst.setIndicatorColor(Color.BLUE);
        pst.setIndicatorHeight(5);
        pst.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        sld.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        break;
                    default:

                        sld.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private class MyPagerAdapter extends LazyFragmentPagerAdapter {
        final String[] MOVIE_TITLE = {"热点视频", "娱乐视频", "搞笑视频", "精品视频"};
        final String[] MOVIE_PATH = {"V9LG4B3A0", "V9LG4CHOR", "V9LG4E6VR", "00850FRB"};

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return MOVIE_TITLE[position];
        }

        @Override
        protected Fragment getItem(ViewGroup container, int position) {
            return MovieFragment.newInstance(MOVIE_TITLE[position], MOVIE_PATH[position]);
        }

        @Override
        public int getCount() {
            return MOVIE_PATH.length;
        }
    }

}
